package com.dongjiantong.web.protal.controller.farmrecords.bullfarmrecords;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.farmrecords.enums.FarmRecordsNumberStatus;
import com.dongjiantong.farmrecords.model.FarmRecordsImmunization;
import com.dongjiantong.farmrecords.model.FarmRecordsImmunizationSub;
import com.dongjiantong.farmrecords.service.FarmRecordsImmunizationService;
import com.dongjiantong.farmrecords.service.FarmRecordsImmunizationSubService;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.enums.AnimalTypeEnum;
import com.dongjiantong.sys.model.AnimalType;
import com.dongjiantong.sys.service.AnimalTypeService;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 防疫中心-免疫记录表-牛(政府端)
 */

@Slf4j
@OpenBean
public class FarmRecordsBullImmunizationController {

    @Reference(version = "1.0.0")
    public FarmRecordsImmunizationService farmRecordsImmunizationService;
    @Reference(version = "1.0.0")
    public FarmRecordsImmunizationSubService farmRecordsImmunizationSubService;
    @Reference(version = "1.0.3")
    public AnimalTypeService animalTypeService;

    /**
     * 根据养殖场ids,起止时间或者状态查询免疫记录 (条件都可以为空,表示查询所有)(包含对应免疫子表中的数据)
     * @param areaList 请求的地区List
     * @param farmId  请求的养殖场id
     * @param startDate 请求的查询开始时间
     * @param endDate   请求的查询结束时间
     * @param pageNum
     * @param pageSize
     * @param type 免疫类型
     * @param cowNumber 牛号
     * @return  Response<PageInfo<FarmRecordsDisinfect>>
     */
    @OpenMethod(key = "web.farmrecordsimmunization.bull.selectByFarmIdAndDate",
            paramNames = {"areaList","farmId","startDate","endDate","pageNum","pageSize","type","cowNumber"},
            httpMethods = RequestMethod.GET)
    public Response<PageInfo<FarmRecordsImmunization>> selectByFarmIdAndDate(List<Integer> areaList,
                                                                             @RequestParam(required = false) Long farmId,
                                                                             @RequestParam(required = false) String startDate,
                                                                             @RequestParam(required = false) String endDate,
                                                                             @RequestParam(required = false) Integer pageNum,
                                                                             @RequestParam(required = false) Integer pageSize,
                                                                             @RequestParam(required = false) Integer type,
                                                                             @RequestParam(required = false) String cowNumber){
        if (farmRecordsImmunizationService == null){
            throw new OPServerException("farmRecordsImmunizationService 为null,请检查");
        }
        if (farmRecordsImmunizationSubService == null){
            throw new OPServerException("farmRecordsImmunizationSubService 为null,请检查");
        }
        if (animalTypeService == null){
            throw new OPServerException("animalTypeService 为null,请检查");
        }
        Map map = Maps.newHashMap();
        Long userId = UserUtil.getUserId();
        map.put("userId",userId);
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }

        if (farmId != null){
            //如果养殖场id不为空,则根据养殖场id查询
            map.put("farm_id",farmId);
        }else {
            //如果养殖场id为空,判断地区code是否为空,不为空则根据地区查询,否则查询登陆人管辖范围的记录
            if (areaList.size() > 0){
                int i = 0;
                for (Integer areaCode : areaList) {
                    if (i == 0) {
                        map.put("province_id",areaCode);
                    } else if (i == 1) {
                        map.put("city_id",areaCode);
                    } else if (i == 2) {
                        map.put("region_id",areaCode);
                    } else if (i == 3) {
                        map.put("street_id",areaCode);
                    } else {
                        map.put("village_id",areaCode);
                    }
                    i++;
                }
            }
        }
        if (!Strings.isNullOrEmpty(startDate)){
            Date startDate1 = new Date(Long.valueOf(startDate));
            map.put("startDate",startDate1);
        }
        if (!Strings.isNullOrEmpty(endDate)){
            Date endDate1 = new Date(Long.valueOf(endDate));
            map.put("endDate",endDate1);
        }

        map.put("breed_second_type",AnimalTypeEnum.BULL.getCode());

        if (type != null){
            map.put("type",type);
        }
        if (!Strings.isNullOrEmpty(cowNumber)){
            map.put("cow_number",cowNumber);
        }

        PageInfo<FarmRecordsImmunization> farmRecordsImmunizationPageInfo = farmRecordsImmunizationService.selectBullByMap(pageNum,pageSize,map);
        return Response.ok(farmRecordsImmunizationPageInfo);
    }



    /**
     * 根据免疫主表 主键id查询对应免疫记录
     * @param id
     * @return
     */
    @OpenMethod(key = "web.farmrecordsimmunization.bull.selectById",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET)
    public Response<FarmRecordsImmunization> selectById(@RequestParam(required = false)Long id){
        if (id == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "请求的id不能为空");
        }
        FarmRecordsImmunization farmRecordsImmunization = farmRecordsImmunizationService.selectByIdWeb(id);
        if (farmRecordsImmunization == null) {
            throw new OPServerException("查无数据");
        }
        return Response.ok(farmRecordsImmunization);
    }

    /**
     * 根据免疫子表id 查询一条免疫记录信息(即 首免或者二免,三免之类)
     * @param id
     * @return
     */
    @OpenMethod(key = "web.farmrecordsimmunization.bull.selectBySubId",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET)
    public Response<FarmRecordsImmunizationSub> selectBySubId(@RequestParam(required = false) Long id){
        if (id == null){
            throw new OPServerException(HttpCode.BAD_REQUEST,"请求参数id不能为空");
        }
        FarmRecordsImmunizationSub farmRecordsImmunizationSub = farmRecordsImmunizationSubService.selectBySubId(id);
        if (farmRecordsImmunizationSub == null) {
            throw new OPServerException("查无数据");
        }
        return Response.ok(farmRecordsImmunizationSub);
    }

    /**
     * 根据免疫主表 主键id删除对应免疫记录 (包括子表的免疫记录)
     * @param id
     * @return
     */
    @OpenMethod(key = "web.farmrecordsimmunization.bull.deleteById",
            paramNames = {"id"},
            httpMethods = RequestMethod.POST)
    public Response<Boolean> deleteById(@RequestParam(required = false)Long id){
        if (id == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST, "请求id不能为空");
        }
        try {
            FarmRecordsImmunization farmRecordsImmunization = new FarmRecordsImmunization();
            farmRecordsImmunization.setId(id);
            farmRecordsImmunization.setDelete_flag(FarmRecordsNumberStatus.INVALID.getCode());
            farmRecordsImmunization.setUpdated_at(new Date());

            //todo 更新免疫子表的数据
            int n = farmRecordsImmunizationSubService.updateDeleteFlag(id);
            int i = farmRecordsImmunizationService.updateByPrimaryKeySelective(farmRecordsImmunization);

            return Response.ok(i == 0 ? false : true);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.DATA_DELETE_FAIL,HttpCode.DATA_DELETE_FAIL.getMessage());
        }
    }
}
